#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
typedef int ElemType;
typedef struct SqList{
    ElemType *pList;
    int listSize;
    int length;
}SqList;

void init(SqList* L){
    
   L->pList = (ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
   L->listSize = MAXSIZE;
   L->length = 0;
   
}
//删除 ith 元素
int deleteElem(SqList *L,int ith){
    if(ith>L->length || ith<1){
        return 0;
    }
    for (int i = ith-1; i < L->length-1; i++)
    {
        L->pList[i] = L->pList[i+1];
    }
    L->length--;
}
//在第i个元素前插入e
int insert(SqList *L,int index,ElemType e){
    if(index <1 || index >L->length){
        return 0;
    }
    if(L->length ==L->listSize){
        //满了，需要扩容
        ElemType *pNew =  (ElemType *)malloc(sizeof(ElemType)*MAXSIZE*2);
        for (int i = 0; i < L->length; i++)
        {
           //复制元素的值
           pNew[i] = L->pList[i];
        }
        L->pList = pNew;
        L->listSize = MAXSIZE*2;
    }
    //插入元素
    for (int i = L->length; i >= index-1; i--)
    {
        L->pList[i] = L->pList[i-1];
    }
    L->pList[index-1] = e;
    L->length++;
    return 1;
}
//删除最小值，并让最后一个元素覆盖
int deleteMin(SqList*L){
    int minInex = 0;
    for (int i = 1; i < L->length; i++)
    {
        if(L->pList[i] < L->pList[minInex]){
            minInex = i;
        }
    }
    L->pList[minInex]  = L->pList[L->length-1];
    L->length--;
}

//删除s-t之间的元素  使用过滤法
int deleteElems(SqList *L,int s,int t){
    int curlength = 0;
    for (int i = 0; i < L->length; i++)
    {
       if(L->pList[i] <s || L->pList[i] >t ){
        //符合我们的筛选条件
        L->pList[curlength++] =  L->pList[i];
       }
    }
    L->length = curlength;
    return 1;
}

//非递减序列删除s-t  偏移
int deleteA(SqList* L,int s, int t){
    int start = 0;
    int last = L->length-1;
    for (int i = 0; i < L->length; i++)
    {
        if(L->pList[i]>=s && L->pList[i]<=t){
            start = i;
            break;
        }
    }
     for (int i = last; i >= 0; i--)
    {
        if(L->pList[i]>=s && L->pList[i]<=t){
            last = i;
            break;
        }
    }
    int k = last -start +1;
    for (int i = last +1; i < L->length; i++)
    {
       L->pList[i-k]  = L->pList[i];
    }
    L->length -=k;
    return 1;   
}

//删除非递减重复元素
void deleteR(SqList*L){
    int curLength = 1;
    for (int i = 1; i < L->length; i++)
    {
      if(L->pList[i]!=L->pList[curLength-1]){
         L->pList[curLength++] =  L->pList[i];
      }
    }
    L->length = curLength;

}

//将升序表A，降序表B合并为升序表C
int combine(SqList *La,SqList*Lb,SqList*Lc){
    Lc->pList = (ElemType*)malloc(sizeof(ElemType)*(La->length+Lb->length));
    Lc->listSize = La->length+Lb->length;
    Lc->length = 0;
    int length = 0;
    int i = 0, j=Lb->length-1;
    while (i <La->length && j>=0)
    {
       if(La->pList[i] > Lb->pList[j]){
            if(Lc->pList[length-1] !=  Lb->pList[j]){
            Lc->pList[length++] =  Lb->pList[j];
            }
            j--;
       }else if (La->pList[i] < Lb->pList[j])
       {       
        if(Lc->pList[length-1] != La->pList[i]){
            Lc->pList[length++] =  La->pList[i];
        }
              i++;
       }else{
        if(Lc->pList[length-1] != La->pList[i]){
             Lc->pList[length++] =  La->pList[i];  
        }
         i++,j--;
       }
    }
    while (i <La->length)
    {
        Lc->pList[length++] =  La->pList[i];
            i++;
    }

    while (j>=0)
    {
          Lc->pList[length++] =  Lb->pList[j];
            j--;
    }
    
    Lc->length = length;
    

} 
//将A和B合并到表A,升序
int combineByMax(SqList *La,SqList *Lb){
    int i = La->length-1;
    int j = 0;
    int curLength = 0;//新插入最后的元素个数
    while (i>=0 && j<Lb->length)
    {
      if(La->pList[i] > Lb->pList[j]){
            La->pList[La->listSize-1-curLength] =La->pList[i] ;
            i--;
      }else{
            La->pList[La->listSize-1-curLength] =Lb->pList[j] ;
            j++;
      }
      curLength++;
    }
    //如果A有剩余就不需要再遍历， 符合升序
    while (j<Lb->length)
    {
        La->pList[La->listSize-1-curLength] =Lb->pList[j] ;
        j++;
        curLength++;
    }
    La->length +=Lb->length;
    return 1;
    
}
//插入排序，部分
int insertSort(SqList *La,int r){
    int i,j;
    //如果是完整的插入排序，让i为1开始
    for ( i = r; i < La->length; i++)
    {
        int temp = La->pList[i];
        for (j = i-1; j >= 0;j--)
        {
           if(La->pList[j] <= temp){
                break;
           }
           La->pList[j+1] = La->pList[j];
        }
        La->pList[j+1] = temp;
        
    }
    return 1;
}

//求A和B的交集 ，升序
int insertsect(SqList *La,SqList *Lb){
    int curLength = 0;
    int i=0, j =0;
    while (i<La->length && j<Lb->length)
    {
       if(La->pList[i] > Lb->pList[j]){
            j++;
       }else if (La->pList[i] < Lb->pList[j])
       {
        i++;
       }else{
            La->pList[curLength++] = La->pList[i];
            i++;j++;
       }
    }

    La->length = curLength;
    return 1;
    
}

//求A和B的差集 ，升序
int except(SqList *La,SqList *Lb,SqList *Lc){
        int curLength= 0;
        int i=0,j=0;
        while (i<La->length && j<Lb->length)
        {
       if(La->pList[i] > Lb->pList[j]){
            Lc->pList[curLength++] =  Lb->pList[j];
            j++;
       }else if (La->pList[i] < Lb->pList[j])
       {
         Lc->pList[curLength++] =  La->pList[i];
            i++;
       }else{
           
            i++;j++;
       }
        }

        while (i<La->length )
        {
            Lc->pList[curLength++] =  La->pList[i];
            i++;
        }
        
        while (j<Lb->length)
        {
            Lc->pList[curLength++] =  Lb->pList[j];
            j++;
        }

        Lc->length = curLength;
        return 1;
        
}

int reverse(SqList *La){
    int i = 0;
    int j = La->length-1;
    while (i<j)
    {
       int temp = La->pList[i];
       La->pList[i] = La->pList[j];
       La->pList[j] = temp;
       i++; j--;
    }
    return 1;
    
}
//删除位于min-max范围的节点
void deleteElemsList(){

}
void print(SqList * L){
    printf("this Sqlist : length is %d , listSize = %d \n",L->length,L->listSize);
    for (int i = 0; i < L->length; i++)
    {
        printf(" %d ",L->pList[i]);
    }
    printf("\n");
}
int main(){
    SqList La,Lb,Lc;
    
    init(&La);
    int a[10] = {1,2,5,6,9};
    int b[5] = {3,4,5,6,7};
    int c[10] = {0};
    Lb.pList = b;
    Lb.length = 5;
    La.pList = a;
    La.length = 5;
    Lc.length = 0;
    Lc.listSize = 10;
    Lc.pList = c;
   reverse(&La);

    
    print(&La);
  
    
}